package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.OpenAreaDao;
import cn.edu.sdjzu.xg.bysj.domain.OpenArea;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public class OpenAreaService {
    private static OpenAreaService openAreaService = new OpenAreaService();
    private OpenAreaDao openAreaDao = OpenAreaDao.getInstance();
    private OpenAreaService(){}
    public static OpenAreaService getInstance(){
        return openAreaService;
    }

    /**
     * 添加课题
     * @param openArea
     * @return
     * @throws SQLException
     */
    public boolean add(OpenArea openArea) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean added = openAreaDao.add(connection,openArea);
        JdbcHelper.close(connection);
        return added;
    }

    /**
     * 根据id查找课题
     * @param id
     * @return
     * @throws SQLException
     */
    public OpenArea find(int id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        OpenArea openArea = openAreaDao.find(connection,id);
        JdbcHelper.close(connection);
        return openArea;
    }

    /**
     * 条件分页查找课题
     * @param conditions
     * @param pagination
     * @return
     * @throws SQLException
     */
    public Collection<OpenArea> findAll(Collection<Condition> conditions , Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<OpenArea> openAreas = openAreaDao.findAll(connection,conditions,pagination);
        JdbcHelper.close(connection);
        return openAreas;
    }

    /**
     * 删除课题
     * @param id
     * @return
     * @throws SQLException
     */
    public boolean delete(int id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = openAreaDao.delete(connection,id);
        JdbcHelper.close(connection);
        return deleted;
    }

    /**
     * 更新课题
     * @param openArea
     * @return
     * @throws SQLException
     */
    public boolean update(OpenArea openArea) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = openAreaDao.update(connection,openArea);
        JdbcHelper.close(connection);
        return updated;
    }
}
